// run: $exec < input
#include <iostream>

int const maxn = 100007;
int const maxh = 1000007;
long long tree[maxh];
long long tree2[maxh];
long long a[maxn];
int n, k;

int lowbit(int x) { return x & -x; }

void update(int id, int d, long long tree[])
{
	for (; id < maxh; id += lowbit(id)) tree[id] += d;
}

long long get_sum(int id, long long tree[])
{
	long long ret = 0;
	for (; id; id -= lowbit(id)) ret += tree[id];
	return ret;
}

long long get_mid()
{
	int l = 1, r = maxh - 1, t = k/2 + (k & 1);
	while (l + 1 < r) {
		int mid = (l + r) / 2;
		if (get_sum(mid, tree) >= t) r = mid;
		else l = mid;
	}
	if (get_sum(l, tree) == t) r = l;
	return r;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n >> k;
	for (int i = 1; i <= n; i++) { std::cin >> a[i]; a[i]++; }
	for (int i = 1; i < k; i++) {
		update(a[i], 1, tree);
		update(a[i], a[i], tree2);
	}
	long long ans = (1ll) << 60;
	for (int i = k; i <= n; i++) {
		update(a[i], 1, tree);
		update(a[i], a[i], tree2);

		int mid = get_mid();
		long long ts = get_sum(mid, tree2), num = get_sum(mid, tree);
		long long tmp = num * mid - ts;
		tmp += get_sum(maxh, tree2) - ts - (k - num) * mid;
		ans = std::min(ans, tmp);
		update(a[i - k + 1], -1, tree);
		update(a[i - k + 1], -a[i - k + 1], tree2);
	}
	std::cout << ans << '\n';
}

